//
//  HRHearOfTopicListViewController.m
//  Hear
//
//  Created by mac on 2018/1/15.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "HRHearOfTopicListViewController.h"
#import "HRHearOfTopicCell.h"
#import "HRGetSoundQuestionsViewModel.h"
#import "MJRefresh.h"
#import "HRTopicInfoViewController.h"
#import "HRGetSoundQuestionsModel.h"
@interface HRHearOfTopicListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)NSMutableArray *conentArr;
@property(nonatomic, strong)HRGetSoundQuestionsViewModel *viewModel;
@property (nonatomic, assign)NSInteger pIndex;

@end

@implementation HRHearOfTopicListViewController
#pragma mark - Get and Set
- (HRGetSoundQuestionsViewModel *)viewModel{
    if (_viewModel == nil) {
        _viewModel = [[HRGetSoundQuestionsViewModel alloc] init];
    }
    return _viewModel;
}

- (NSMutableArray *)conentArr{
    if (_conentArr == nil) {
        _conentArr = [[NSMutableArray alloc] init];
    }
    return _conentArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    
    [self getSoundQuestionsList];
    // Do any additional setup after loading the view.
}



- (void)setupView{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 70;
    self.tableView.backgroundColor = CustomColor(240, 240, 240, 1);
    if (@available(iOS 11.0, *))
    {
        self.tableView.estimatedRowHeight = 0;
    }
    
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.conentArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HRHearOfTopicCell *cell = [HRHearOfTopicCell cellWithTableView:tableView];
    cell.model = self.conentArr[indexPath.row];
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRTopicInfoViewController *VC = [HRTopicInfoViewController new];
    VC.model = self.conentArr[indexPath.row];
    [self.navigationController pushViewController:VC animated:YES];
}


- (void)getSoundQuestionsList{
    self.pIndex = 0;
    
    [self getSoundQuestionsList:YES];
    WeakSelf
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pIndex = 0;
        [self getSoundQuestionsList:YES];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
    //    //上拉加载
    self.tableView.mj_footer=[MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        weakSelf.pIndex ++;
        [weakSelf getSoundQuestionsList:NO];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}


- (void)getSoundQuestionsList:(BOOL)isHead{
    WeakSelf
    NSMutableDictionary *pramDict = [NSMutableDictionary dictionary];
    pramDict[@"CurrentUserLocalId"] = USERID;
    pramDict[@"IsSelf"] = @"1";
    pramDict[@"PageSize"] = @"20";
    pramDict[@"PageIndex"] = [NSString stringWithFormat:@"%d",self.pIndex];
    [self.viewModel getSoundQuestions:pramDict andUrl:@"SoundInfo/Get_GetSoundQuestionsList" resultArray:^(NSMutableArray *resultArray) {
        if (resultArray){
            if (isHead){
                [weakSelf.conentArr removeAllObjects];
            }
            [weakSelf.conentArr addObjectsFromArray:resultArray];
            [weakSelf.tableView reloadData];
            if(resultArray.count <20){
                weakSelf.tableView.mj_footer.hidden = YES;
            }
        }
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
